package com.yanqu.road.logic.helper;

import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class RandomWeightConfigHelper {
    private static RandomHelper randomHelper = new RandomHelper();
    public static Property getRandomProperty(String param) {
        Property reward = new Property();
        if (param != null) {
            try {
                List<WeightConfig> configList = new ArrayList<>();
                String[] arr = param.split("\\|");
                int sumWeight = 0;
                for (String s : arr) {
                    String[] split = s.split(";");
                    if(split.length >= 2){
                        Property property = PropertyHelper.parseStringToProperty(split[0]);
                        int weight = Integer.parseInt(split[1]);
                        WeightConfig weightConfig = new WeightConfig(weight, property);
                        configList.add(weightConfig);
                        sumWeight += weight;
                    }
                }
                if (configList.isEmpty()) {
                    return reward;
                }
                int num = randomHelper.next(0, sumWeight);
                for (WeightConfig config : configList) {
                    int weight = config.getWeight();
                    if (num < weight) {
                        return config.getProperty();
                    }
                    num -= weight;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return reward;
    }

    public static Property getWeightProperty(List<WeightConfig> list){
        Property reward = new Property();
        int sumWeight = 0;
        for (WeightConfig item : list) {
            sumWeight += item.weight;
        }
        int num = randomHelper.next(0, sumWeight);
        for (WeightConfig config : list) {
            int weight = config.getWeight();
            if (num < weight) {
                reward = config.getProperty();
                break;
            }
            num -= weight;
        }
        return reward;
    }

    public static Property getPropertyWeightItem(List<PropertyWeightItem> list){
        Property reward = new Property();
        int sumWeight = 0;
        for (PropertyWeightItem item : list) {
            sumWeight += item.getWeight();
        }
        int num = randomHelper.next(0, sumWeight);
        for (PropertyWeightItem config : list) {
            int weight = config.getWeight();
            if (num < weight) {
                reward = config.getProperty();
                break;
            }
            num -= weight;
        }
        return reward;
    }

    public static Property getPoolWeightDrawResult(Map<Integer, List<PropertyWeightItem>> weightItemMap) {
        Property resultProperty = new Property();
        if (weightItemMap == null) {//配置有问题
            return resultProperty;
        }
        for (Map.Entry<Integer, List<PropertyWeightItem>> entry : weightItemMap.entrySet()) {
            if (entry.getValue().size()==0){//配置有问题
                return resultProperty;
            }
            if (entry.getValue().size()==1){    //只有一个元素 该池子必拿
                resultProperty.addProperty(entry.getValue().get(0).getProperty());
                continue;
            }
            Property item = getPropertyWeightItem(entry.getValue());
            resultProperty.addProperty(item);
        }
        return resultProperty;
    }

    public static class WeightConfig{
        private int weight;
        private Property property;

        public WeightConfig(int weight, Property property) {
            this.weight = weight;
            this.property = property;
        }

        public int getWeight() {
            return weight;
        }

        public Property getProperty() {
            return property;
        }
    }

    public static void main(String[] args) {
        String str = "119019=5;123389=40,1#110159=2,1|119019=5,1#110143=1,1";
        Property result = getProperty(str);
        System.out.println(PropertyHelper.parsePropertyToString(result));


        str = "119019=5,1|119019=5,1";
        result = getProperty(str);
        System.out.println(PropertyHelper.parsePropertyToString(result));


        str = "119019=5;123389=40,1";
        result = getProperty(str);
        System.out.println(PropertyHelper.parsePropertyToString(result));


        str = "119019=5;123389=40,1|119019=5,1";
        result = getProperty(str);
        System.out.println(PropertyHelper.parsePropertyToString(result));
    }

    public static Property getProperty(String rewardConfig){
        Random random = new Random();
        return getProperty(rewardConfig, random);
    }

    public static Property getProperty(String rewardConfig, Random random) {
        List<String> poolList = StringUtils.stringToStringList(rewardConfig, "\\|");
        Property result = new Property();
        for (String poolString : poolList) {
            List<String> randomList = StringUtils.stringToStringList(poolString, "#");
            if(randomList.size() == 0){
                continue;
            }
            if(randomList.size() == 1){
                List<String> itemWeight = StringUtils.stringToStringList(randomList.get(0), ",");
                if(itemWeight.size() > 0){
                    result.addProperty(PropertyHelper.parseStringToProperty(itemWeight.get(0)));
                }
            }else {
                //按权重随机
                List<String> rewardList = new ArrayList<>();
                List<Integer> weightList = new ArrayList<>();
                for (String itemString : randomList) {
                    List<String> itemWeightList = StringUtils.stringToStringList(itemString, ",");
                    if(itemWeightList.size() == 2){
                        rewardList.add(itemWeightList.get(0));
                        weightList.add(Integer.parseInt(itemWeightList.get(1)));
                    }
                }
                int index = RandomHelper.getRandomIndexByWeight(weightList, random);
                result.addProperty(PropertyHelper.parseStringToProperty(rewardList.get(index)));
            }
        }
        return result;
    }
}
